<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/layer-css.css"/>
    <style>
        .layui-form-item .layui-inline {
            margin-right: 0px !important;
            margin-bottom: 0px !important;
        }
        .search-form .pear-btn{
            height: 38px;
            line-height: 38px;
            font-size: 16px;
        }
        .search-form .pear-btn i {
            font-size: 16px;
        }
        .layui-form-item {
            margin-bottom: 8px;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form search-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">档号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="reference" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">档案名字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">案卷号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileNo" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">年度</label>
                    <div class="layui-input-inline">
                        <select name="year" lay-search>
                            <option value="">请选择年度</option>
                            <option th:each="year,userStat : ${yearList}" th:value="${year}"
                                    th:text="${year}">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">责任人</label>
                    <div class="layui-input-inline">
                        <select name="userId" lay-search>
                            <option value="">请选择责任人</option>
                            <option th:each="user,userStat : ${userList}" th:value="${user.userId}"
                                    th:text="${user.userName}">
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">选择机柜</label>
                    <div class="layui-input-inline">
                        <select name="number" lay-search lay-filter="cabinetChange">
                            <option value="">请选择机柜编号</option>
                            <option th:each="dict:${hostList}" th:value="${dict.number}" th:text="${dict.number}">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">请选择层</label>
                    <div class="layui-input-inline">
                        <select name="layer" lay-search>
                            <option value="">请选择层</option>
                            <option th:each="layer : ${layerList}" th:value="${layer}" th:text="${layer}">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">请选择列</label>
                    <div class="layui-input-inline">
                        <select name="column" lay-search>
                            <option value="">请选择列</option>
                            <option th:each="column : ${columnList}" th:value="${column}" th:text="${column}">
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">时间</label>
                    <div class="layui-inline" id="test6">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="startDate" name="startDate" class="layui-input"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" id="endDate" name="endDate" class="layui-input"
                                   placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <!--                <div class="layui-form-item layui-inline">-->
                <!--                    <label class="layui-form-label">文档检索</label>-->
                <!--                    <div class="layui-input-inline">-->
                <!--                        <input type="text" name="pdfSearch" placeholder="" class="layui-input">-->
                <!--                    </div>-->
                <!--                </div>-->
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
                <li class="layui-this validArchives">档案</li>
                <li class="batchReceipt">批量入库</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table id="validArchives" lay-filter="validArchives"></table>
                </div>
                <div class="layui-tab-item">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                        <legend>批量上传</legend>
                    </fieldset>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn layui-btn-normal" id="fileList">选择多文件</button>
                        <a href="/file/manage/download/template">
                            <button type="button" class="layui-btn" id="downloadTemplate">下载批量入库模版</button>
                        </a>
                        <div class="layui-upload-list" style="max-width: 1000px;">
                            <table class="layui-table">
                                <colgroup>
                                    <col>
                                    <col width="150">
                                    <col width="260">
                                    <col width="150">
                                </colgroup>
                                <thead>
                                <tr>
                                    <th>文件名</th>
                                    <th>大小</th>
                                    <th>上传进度</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="demoList"></tbody>
                            </table>
                        </div>
                        <button type="button" class="layui-btn" id="fileListAction">开始上传</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="validArchives-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增档案
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除档案
    </button>
</script>
<script type="text/html" id="validArchives-bar">
    {{#if (d.status == 0) { }}
    <button plain class="pear-btn pear-btn-success pear-btn-xs" lay-event="takeOut">取出档案</button>&nbsp;&nbsp;
    {{# }else if(d.status == 1){ }}
    <button plain class="pear-btn pear-btn-success pear-btn-xs" lay-event="returnFile">归还档案</button>&nbsp;&nbsp;
    {{# } }}
    <button plain class="pear-btn pear-btn-primary  pear-btn-xs" lay-event="updateFile">修改档案</button>&nbsp;&nbsp;
    <button plain class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除档案</button>
</script>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate', 'element'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let upload = layui.upload;
        let laydate = layui.laydate;
        let element = layui.element;
        //日期范围
        laydate.render({
            elem: '#test6',
            //设置开始日期、日期日期的 input 选择器
            //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
            range: ['#startDate', '#endDate'],
            max: maxDate(),
            theme: '#000000'
        });

        // 设置最大可选的日期
        function maxDate() {
            var now = new Date();
            return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
        }


        let validCols = [
            [
                {type: 'checkbox'},
                {title: '档号', field: 'reference', align: 'center'},
                {title: '档案名称', field: 'file_name', align: 'center'},
                {title: '案卷号', field: 'file_no', align: 'center'},
                {title: '年度', field: 'year', align: 'center'},
                {title: '责任人', field: 'user_name', align: 'center'},
                {title: '机柜编号', field: 'number', align: 'center'},
                {title: '层', field: 'current_layer', align: 'center', sort: true,},
                {title: '列', field: 'current_column', align: 'center', sort: true,},
                {
                    title: '档案状态', field: 'status', align: 'center', templet: function (d) {
                        if (d.status == 0) {
                            return '<span style="color: #069b23;">未取出</span>';
                        } else {
                            return '<span style="color: #e30707;">已取出</span>'
                        }
                    }
                },
                {
                    title: '电子档案', field: 'electron_file', align: 'center', minWidth: 120, templet: function (d) {
                        if (!d.electron_file) {
                            return '<button class="pear-btn pear-btn-primary pear-btn-xs upload_btn" value="' + d.id + '" style="border: 1px solid #31bdec !important;' +
                                '    background-color: #31bdec !important;">录入</button>';
                        } else {
                            return '<button class="pear-btn pear-btn-success pear-btn-xs " ' +
                                'lay-event="viewElectronicFiles">查看档案</button>';
                        }
                    }
                },
                {title: '操作', fixed: 'right', align: 'center', minWidth: 300, toolbar: '#validArchives-bar'}
            ]
        ]
        $(".validArchives").click(function () {
            validArchives();
        })

        function validArchives() {
            table.render({
                elem: '#validArchives',
                url: '/file/manage/list',
                // height: 'full-230',
                page: true,
                limits: [40],
                limit: 40,
                cols: validCols,
                skin: 'line',
                toolbar: '#validArchives-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports'],
                id: 'valid',
                text: {
                    none: '暂无相关数据' //默认：无数据。
                }, done: function (res, curr, count) {//表格加载完成后，对上传按钮进行渲染
                    var tableId = this.id;
                    var tableElem = this.elem;
                    //table render出来实际显示的table组件
                    var tableViewElem = tableElem.next();
                    //渲染当前页面的所有upload组件
                    upload.render({
                        elem: tableViewElem.find('.upload_btn')
                        , url: '/file/manage/upload/electron/file'
                        , accept: 'file'//允许上传的文件类型
                        , exts: 'pdf|jpg|png'//只允许上传jpg和png图片格式
                        , before: function (obj) {
                            var tableElem = this.item;
                            archivesId = tableElem.attr('value');
                            layer.load(); //上传loading
                        }
                        , data: {
                            archivesId: function () {
                                return archivesId;
                            }
                        }
                        , done: function (res) {
                            layer.closeAll('loading'); //关闭loading
                            layer.msg('上传成功');
                            table.reload('valid');
                        }
                        , error: function (index, upload) {
                            layer.closeAll('loading'); //关闭loading
                            layer.msg('上传失败，请重试');
                        }
                    });
                }
            });
        }

        validArchives();
        table.on('tool(validArchives)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'takeOut') {
                window.takeOut(obj);
            } else if (obj.event === 'returnFile') {
                window.returnFile(obj);
            } else if (obj.event === 'updateFile') {
                window.updateFile(obj);
            } else if (obj.event === 'uploadElectronFile') {
                window.uploadElectronFile(obj);
            } else if (obj.event === 'viewElectronicFiles') {
                window.viewElectronicFiles(obj);
            }
        });
        table.on('toolbar(validArchives)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });
        // 查看电子档案
        window.viewElectronicFiles = function (obj) {
            console.log(obj.data);
            parent.layer.open({
                type: 2,
                title: '查看电子档案',
                shadeClose: true,
                maxmin: true, //开启最大化最小化按钮
                area: ['1000px', '700px'],
                content: '/electron/archives/table?archivesId=' + obj.data.id,
            });
        }
        // 上传电子档案（对接高拍仪）
        window.uploadElectronFile = function (obj) {
            layer.open({
                type: 2,
                title: '上传电子档案',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '400px'],
                content: '/file/manage/upload/electron?id=' + obj.data['id'],
                success: function (layero, index) {
                    layer.full(index); // 最大化
                }
            });
        }
        //搜索
        form.on('submit(search)', function (data) {
            table.reload('valid', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: data.field
            })
            return false;
        });
        window.add = function () {
            parent.layer.open({
                title: ['新增档案', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/file/manage/insert',
                end: function () {
                    table.reload("valid")
                }
            });
        }
        //批量删除档案
        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            var fileNameList = common.checkField(obj, 'file_name');
            layer.confirm('<div>确定要删除&nbsp;&nbsp;<span style="color: #FF9200;">' + fileNameList + '</span>&nbsp;&nbsp;档案吗?</div>', {
                icon: 3,
                title: ['删除确认', 'font-size: 18px;'],
                shade: 0.5,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/file/manage/delete/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {
                                icon: 1,
                                time: 3000
                            }, function () {
                                table.reload("valid");
                            });
                        } else {
                            layer.msg(result.message, {
                                icon: 2,
                                time: 3000
                            });
                        }
                    }
                })
            });
        }

        window.takeOut = function (obj) {
            layer.confirm('<div>确定要取出&nbsp;&nbsp;<span style="color: #FF9200;">' + obj.data.file_name + '</span>&nbsp;&nbsp;档案吗?</div>', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                layer.open({
                    type: 2,
                    title: '取出档案信息录入',
                    shade: 0.1,
                    maxmin: true, //开启最大化最小化按钮
                    area: [common.isModile() ? '100%' : '580px', common.isModile() ? '100%' : '550px'],
                    // area: 'auto',
                    content: '/file/manage/takeout/view?fileId=' + obj.data.id
                });
            });
        }
        // 归还档案
        window.returnFile = function (obj) {
            layer.open({
                type: 2,
                title: '归还档案',
                shade: 0.1,
                maxmin: true, //开启最大化最小化按钮
                area: [common.isModile() ? '100%' : '594px', common.isModile() ? '100%' : '570px'],
                content: '/file/manage/returnFile?uuid=' + obj.data.uuid
            });
        }
        // 删除档案
        window.remove = function (obj) {
            layer.confirm('<div>确定要删除&nbsp;&nbsp;<span style="color: #FF9200;">' + obj.data.file_name + '</span>&nbsp;&nbsp;档案吗?</div>', {
                icon: 3,
                title: ['提示', 'font-size: 18px;'],
                shade: 0.5,
                area: ['499px', '303px'],
                id: "borrowingConfirm"
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/file/manage/delete/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code === 200) {
                            layer.msg(result.message, {icon: 1, time: 1800}, function () {
                                obj.del();
                                table.reload("valid");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    },error:function(result){
                        layer.close(loading);
                        if (result.status === 403) {
                            layer.msg(result.responseText, {icon: 2, time: 2000});

                        }
                    }
                })
            });
        }
        // 修改档案
        window.updateFile = function (obj) {
            parent.layer.open({
                title: ['修改档案', 'font-size: 18px;'],
                type: 2,
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                area: ['40%', '100%'],
                shade: 0.5,
                shadeClose: true,
                maxmin: true,
                id: 'addNewEquipment',
                content: '/file/manage/edit?id=' + obj.data['id'],
                end: function () {
                    table.reload("valid")
                }
            });
        }

        window.refresh = function (param) {
            table.reload('valid');
        }

        //多文件列表
        var uploadListIns = upload.render({
            elem: '#fileList'
            , elemList: $('#demoList') //列表元素对象
            , url: '/file/manage/upload/' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            , accept: 'file'
            , multiple: true
            , number: 3
            , auto: false
            , exts: "xls|xlsx"
            , bindAction: '#fileListAction'
            , choose: function (obj) {
                var that = this;
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                        , '<td><div class="layui-progress" lay-filter="progress-demo-' + index + '"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    that.elemList.append(tr);
                    element.render('progress'); //渲染新加的进度条组件
                });
            }
            , done: function (res, index, upload) { //成功的回调
                var that = this;
                if (res.code == 200) { //上传成功
                    var tr = that.elemList.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(3).html(''); //清空操作
                    delete this.files[index]; //删除文件队列已经上传成功的文件
                    layer.msg(res.message, {icon: 1, time: 3000});
                    return;
                }
                this.error(index, upload);
            }
            , allDone: function (obj) { //多文件上传完毕后的状态回调
                console.log(obj)
            }
            , error: function (index, upload) { //错误回调
                var that = this;
                var tr = that.elemList.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
            , progress: function (n, elem, e, index) { //注意：index 参数为 layui 2.6.6 新增
                element.progress('progress-demo-' + index, n + '%'); //执行进度条。n 即为返回的进度百分比
            }
        });
    })
</script>
</body>
</html>
